﻿/*
 * TextServices / TSDotNet / TSNative
 * Marco Maria Bellocchi, Mauro Bampo
 * Copyright(c) 2013.
 * 
 * The MIT License
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 */

using System;
using TSDotNet.Encoders;

namespace TSDotNet.Tests
{
    class Program
    {

        static void Main(string[] args)
        {
            //new GarbageBeep();
            //between 7.5 and 8.50,0,-421.00,0,-2238.431812,0,
            string[] numbers = new[]
                                   {
                                       "0.91,-13394.000594,0.45,0,0,0-25206.111758,+31215.922354,41334.54218,-51816.167462,-66504.590744,-76135.131954,-92066.85092,-108795.383934,-117840.003658,-121399.473068,-130803.016416,-137750.956066,-148581.152292,-162206.598108,-174252.120814,-171964.879214,-176701.18506,-176885.451566,-181012.944746,-196608.356758,-205358.963636,-266085.341904,-303523.967672,-342422.403662,-374867.27061,-401375.034054,-457094.579386,-502268.466202,-526026.073054,-552884.766906,-567840.806426,-564791.081126,-593550.873594,-632752.471606,-678405.355556,-709651.498896,-748208.740946,-768746.908636,-744767.423288,-773808.877464,-822058.04012,-830124.21368,-883389.536804,-854137.863972,-893386.964366,-881535.718946,-912609.823238,-896619.890396,-928736.538774,-927957.75081,-944515.375324,-962142.701666,-1014604.498522,-1019191.208438,-1027119.38494,-989404.805202,-1008495.278566,-972736.1269,-885537.13764,-712208.111522,-539175.584218,-374383.347286,-207621.239086,-111942.116324,-40685.55953,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0",
                                       "0,0",
                                       "0,0,0,",
                                       "+0,0,0",
                                       "-0,0,0,",
                                       "3-0,0,0",
                                       "3,+0,-0,+0"
                                   };
            TrailingZeroZipper z1 = new TrailingZeroZipper(',', 'Z');
            NumericStringZipper z2 = new NumericStringZipper(new QuickEncoder(), CharByteMappingFactory.CreateLatin1Mapping());
            SimpleCompositeZipper composite = new SimpleCompositeZipper();
            composite.Add(z1);
            composite.Add(z2);
            string zip = null, unzip = null;

            foreach (var s in numbers)
            {
                 zip = composite.Encode(s);
                 unzip = composite.Decode(zip);
            }
            
            int numGC;
            int numOfOp = 1;

            TestCompression(composite.Encode, unzip, numOfOp, out numGC, "composite.Zip");
            TestCompression(composite.Decode, zip, numOfOp, out numGC, "composite.Unzip");
            Console.ReadLine();
        }

        public static double TestCompression(Func<string, string> toRun, string toZip, int numOfZip, out int numOfGarbage, string compressionType)
        {
            int garbages = GarbageBeep.Garbaged;
            DateTime dt = DateTime.Now;
            for (int i = 0; i < numOfZip; i++)
                toRun(toZip);
            DateTime stop = DateTime.Now;
            TimeSpan ts = stop - dt;
            numOfGarbage = GarbageBeep.Garbaged - garbages;
            double seconds = ts.TotalSeconds;
            Console.WriteLine("{0} took {1} and did {2} gc", compressionType, seconds, numOfGarbage);
            return seconds;
        }

        //public static NumericStringZipper GetNumericStringZipper()
        //{
        //    NumericStringEncoder numericStringEncoder = new NumericStringEncoder(',', 12, 'x', 13, 'z', 14);
        //    numericStringEncoder.AddEncoding('1', 1);
        //    numericStringEncoder.AddEncoding('2', 2);
        //    numericStringEncoder.AddEncoding('3', 3);
        //    numericStringEncoder.AddEncoding('4', 4);
        //    numericStringEncoder.AddEncoding('5', 5);
        //    numericStringEncoder.AddEncoding('6', 6);
        //    numericStringEncoder.AddEncoding('7', 7);
        //    numericStringEncoder.AddEncoding('8', 8);
        //    numericStringEncoder.AddEncoding('9', 9);
        //    numericStringEncoder.AddEncoding('0', 10);
        //    numericStringEncoder.AddEncoding('-', 11);
        //    numericStringEncoder.AddEncoding('+', 0);
        //    numericStringEncoder.AddEncoding('.', 15);

        //    Dictionary<char, byte> dictionary = new Dictionary<char, byte>();
        //    for (int i = 0; i < 256; i++)
        //    {
        //        dictionary.Add(CharsHelper.AllChars[i], (byte)i);
        //    }
        //    //SimpleStringCode stringCode = new SimpleStringCode(dictionary);
        //    QuickEncoder encoder = new QuickEncoder();
        //    CastStringCode stringCode = new CastStringCode();
        //    return new NumericStringZipper(encoder, stringCode);
        //}

        //public static IZipper GetTrailingZeroZipper()
        //{
        //    return new TrailingZeroZipper(',', 'z');
        //}


        public class GarbageBeep : IDisposable
        {
            private static int _garbaged;

            public GarbageBeep()
            {

            }

            public static int Garbaged { get { return _garbaged; } }

            protected virtual void Dispose(bool disposing)
            {
                if (disposing)
                {
                    // Free other state (managed objects).

                }
                //Console.Beep();
                new GarbageBeep();
                _garbaged++;
                // Free your own state (unmanaged objects).
                // Set large fields to null.
            }

            // Use C# destructor syntax for finalization code.
            ~GarbageBeep()
            {
                // Simply call Dispose(false).
                Dispose(false);
            }
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
        }
    }
}
